<?php

namespace app\Api\controller;


use Rtgm\sm\RtSm2;

class Service extends ApiBase
{
    public $url;

    public function __construct($url)
    {
        $this->url = config("yst.apiUrlAllinpay") . $url;
    }

    public function assembleRequest($transCode, $jsonString)
    {
        $params = $this->buildCommonParams($transCode, $jsonString);
        $signValue = $this->getSign($params);
        $params['signType'] = 'SM3withSM2';
        $params['sign'] = $signValue;
        file_put_contents('result.log',"请求地址:".json_encode($this->url,JSON_UNESCAPED_UNICODE) .PHP_EOL, FILE_APPEND);
        file_put_contents('result.log',"请求参数:".json_encode($params,JSON_UNESCAPED_UNICODE) .PHP_EOL, FILE_APPEND);
        $res = $this->request($params);
        file_put_contents('result.log',"返回结果:".json_encode($res,JSON_UNESCAPED_UNICODE) .PHP_EOL, FILE_APPEND);
        if (!empty($res)) {
            $resArr = json_decode($res, true);
            if ($this->verify($resArr)) {
                file_put_contents('result.log',"验签结果:成功" .PHP_EOL, FILE_APPEND);
                return $resArr;
            } else {
                file_put_contents('result.log',"验签结果:失败" .PHP_EOL, FILE_APPEND);
            }
        } else {
            file_put_contents('result.log',"接口返回为空" .PHP_EOL, FILE_APPEND);
        }
        return $res;
    }

    public function buildCommonParams($transCode, $json)
    {
        $params = [
            'appId'     => config("yst.appIdAllinpay"),
            'transCode' => $transCode,
            'format'    => 'json',
            'charset'   => 'UTF-8',
            'transDate' => date("Ymd"),
            'transTime' => date("His"),
            'version'   => '1.0',
            'bizData'   => $json,
        ];
        return $params;
    }

    public function getSign($array)
    {
        ksort($array);
        $bufSignSrc = $this->toUrlParams($array);
        $ssm2 = new RtSm2("base64");
        $sign = $ssm2->doSign($bufSignSrc, config("yst.sm2PrivateAllinpay"));
        return trim($sign);
    }

    public function verify($array)
    {
        $sign = $array['sign'];
        unset($array['sign']);
        ksort($array);
        $bufSignSrc = $this->toUrlParams($array);
        $ssm2 = new RtSm2("base64");
        $res = $ssm2->verifySign($bufSignSrc, $sign, config("yst.tlPublicAllinpay"));
        return $res;
    }

    public function toUrlParams(array $array)
    {
        $buff = "";
        foreach ($array as $k => $v) {
            if ($v != "" && !is_array($v)) {
                $buff .= $k . "=" . $v . "&";
            }
        }

        return trim($buff, "&");
    }

    public function request($params)
    {
        $ch = curl_init();
        $this_header = array("content-type: application/json");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params, 256));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public function fileRequest($transCode, $jsonString)
    {
        $params = $this->buildCommonParams($transCode, $jsonString);
        $signValue = $this->getSign($params);
        $params['signType'] = 'SM3withSM2';
        $params['sign'] = $signValue;
        file_put_contents('result.log',"请求地址:".json_encode($this->url,JSON_UNESCAPED_UNICODE) .PHP_EOL, FILE_APPEND);
        file_put_contents('result.log',"请求参数:".json_encode($params,JSON_UNESCAPED_UNICODE) .PHP_EOL, FILE_APPEND);
        $res = $this->request($params);
        file_put_contents('result.log',"返回结果:".json_encode($res,JSON_UNESCAPED_UNICODE) .PHP_EOL, FILE_APPEND);
        if (!empty($res)) {
            $resArr = json_decode($res, true);
            if ($this->verify($resArr)) {
                file_put_contents('result.log',"验签结果:成功" .PHP_EOL, FILE_APPEND);
                return $resArr;
            } else {
                file_put_contents('result.log',"验签结果:失败" .PHP_EOL, FILE_APPEND);
            }
        } else {
            file_put_contents('result.log',"接口返回为空" .PHP_EOL, FILE_APPEND);
        }
        return $res;
    }
}
